var app = getApp();
var request = require('./request.js');
var wx_alert = require('./wx_alert.js');

function bindGetUserInfo(e) {
    console.log(e)
    var that = this;

    if (e.detail.errMsg == "getUserInfo:ok") {
      console.log(e.detail.userInfo);
      var userInfo = JSON.stringify(e.detail.userInfo);
      var data = {
        sessionId: wx.getStorageSync("sessionId"),
        userInfo: userInfo,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      }
      app.globalData.userInfo = e.detail.userInfo;
      request.request(app.globalData.miniLogin,data,function(res){
        console.log(res)
        if(res.code==0){
          wx.setStorageSync("token", res.token);
          wx.setStorageSync("totalInfo", res.user);
          if (res.user.mobile){
            console.log(res.user.mobile)
            wx.setStorageSync("mobile", res.user.mobile);

          }
          that.setData({
            access: "",
          })
        }
      })

    }else{
      wx_alert.msg("您拒绝了小程序的授权");
      that.setData({
        access: "",
      })
    }

}

function next_time() {
  var that = this;
  that.setData({
    access: "",
  })
}
module.exports = {
  bindGetUserInfo: bindGetUserInfo,
  next_time: next_time
}